"use strict";
/* Controllers */
app.controller("costReportListController", ['$scope', '$location', '$rootScope', '$routeParams', 'costReportListService', function ($scope, $location, $rootScope, $routeParams, costReportListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};

    vm.p.type = 'store';
    vm.p.ownerId = $routeParams.username;
    vm.p.period = $routeParams.period;
    vm.p.dt = $routeParams.dt;

    if (vm.p.dt == '0') {
        vm.p.dt = getNowFormatDate(-1);
    }

//    vm.p.period = 'day';
//    vm.d.periodDefines = [
//        'day', 'week', 'month', 'year'
//    ];

//    $scope.setType = function (t) {
//        $scope.vm.d.type = t;
//    };

    var errorFn = function (result) {
        alert(result);
    };

    $scope.loadCostReport = function (t) {
        var vm = $scope.vm;
        $scope.listStoreCostReport();
    };

    $scope.listStoreCostReport = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var param = vm.p;
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            //vm.result.data = data;
            $('#list_table2').bootstrapTable('load', data);
        };
        costReportListService.listCostReport(param, procFn, errorFn);
    };

    $scope.initializeController = function () {
        $scope.vm.result.ms = '';

        var $resultTable2 = $('#list_table2');
        $resultTable2.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable2.bootstrapTable('hideColumn', 'basicPaths');
//        $resultTable2.bootstrapTable('hideColumn', 'updateTime');

        $scope.listStoreCostReport();
    };

    $scope.initializeController();

}]);



/* Services */
app.service('costReportListService', ['ajaxService', function (ajaxService) {

    this.listCostReport = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/cost_report/listCostReport4Owner.do", successFunction, errorFunction);
    };

}]);


function costReportIDFmt(value, row, index) {
    return [
        '<a href="#/star/cost/cost_report_detail/', row.userName, '/', row.period, '/', row.dt, '" target="_blank">', " <u>查看明细</u>", '</a>'
    ].join('');
}

function costStoreReportIDFmt(value, row, index) {
    var optCont1 =
        '<a type="button" class="btn btn-xs btn-primary" href="javascript:void(0);" '
        +'onclick="toDetail(\'' + row.userName + '\',\'' + row.period + '\',\'' + row.dt + '\')">查看明细</a>';
    var optCont2 =
        '<a type="button" class="btn btn-xs btn-primary" href="javascript:void(0);" '
        +'onclick="toChart(\'' + row.userName + '\',\'' + row.period + '\',\'' + row.dt + '\')" >查看趋势</a>';
    return [
        '<div style="width:12rem">',
        optCont1,
        optCont2,
        '</div>'
    ].join('');
}

function toChart(userName, period, ptdate) {

    var basicPath = $('#basicPath option:selected').val()

    if (typeof(basicPath)!='undefined' && basicPath != '') {
        basicPath = basicPath.replaceAll('/', '|')
        window.open('#/star/order/cost_report_detail_chart/owner/' + userName + '/' + basicPath, '_blank');
    } else {
        window.open('#/star/order/cost_report_detail_chart/owner/' + userName+ '/0', '_blank');
    }
}

function toDetail(userName, period, dt) {

    var basicPaths = $('#basicPath option:selected').val()

    if (typeof(basicPaths)!='undefined'  && basicPaths != '') {
        basicPaths = basicPaths.replaceAll('/', '|')
        window.open('#/star/cost/cost_report_detail/' + userName + '/' + period + '/' + dt + '/' + basicPaths, '_blank');
    } else {
        window.open('#/star/cost/cost_report_detail/' + userName + '/' + period + '/' + dt + '/', '_blank');
    }

}



function formatCostUserFmt(value, row, index) {
    var str = value;
    if (value != '' && typeof (value) != 'undefined') {
        if (row.userNameCn != '' && typeof (row.userNameCn) != 'undefined') {
            str = value + '(' + row.userNameCn + ')';
        } else {
            str = value;
        }
    }
    return str;
}

function storeSizeFmt(value, row, index) {
    return internationalNumberShortFormatterStorage(value);
}

// 成本（阿里云）：0.004元/GB/天（≤0.5GB 0.01元/天）（仅作参考）
function storeCostFmt(value, row, index) {
    var gb = value / 1024 / 1024 / 1024;
    if(value<1) {
        return 0+'元';
    }
    if(gb<0.5) {
        return 0.01 + '元';
    }
    return (0.004 * gb * 3 + 0.01).toFixed(2)+'元';
}

// 成本核算单元
function costTeamFmt(value, row, index) {
    if(value==null || value=='') {
        return '-';
    }
    switch(value) {
        case '平台工程技术部/平台数据部/数据技术组/':
        case '平台工程技术部/平台数据部/商分支持组/':
        case '技术支撑线/平台工程技术部/平台数据部/':
            return '数仓团队';

        case '技术支撑线/平台算法技术部/图谱画像策略部/':
        case '技术支撑线/平台算法技术部/业务风控技术部/':
        case '技术支撑线/平台工程技术部/平台工程部/':
        case '技术支撑线/平台算法技术部/连接增长策略部/':
            return '算法团队';

        case '58魔方/产品技术部/数据部/':
        case '58魔方/业务管理运营部/区域运营部/':
        case '人力资源事业群/技术支撑线/魔方技术部/':
            return '魔方团队';

        case '策略支撑线/商业分析部/经营分析部/':
        case '策略支撑线/商业分析部/商业&策略分析部/':
        case '策略支撑线/商业分析部/大服务业务分析部/':
        case '策略支撑线/商业分析部/业务分析部/':
        case '策略支撑线/商业分析部/B&C分析部/':
        case '商业分析部/数据产品部/业务数据产品组/':
            return 'BI团队';

        case '用户业务技术部/主站工程组/兼职业务技术组/':
            return '兼职团队';

        case '技术支撑线/企业业务技术部/企业工程部/':
        case '技术支撑线/企业业务技术部/简历连接部/':
            return 'B侧工程';

        case '技术支撑线/用户业务技术部/赶集工程组/':
        case '用户业务技术部/主站工程组/58业务工程组/':
            return 'C侧工程';

        case '用户成功部/增长产品部/增长运营组/':
        case '用户成功部/增长产品部/客户成功组/':
        case '用户成功部/极速产品部/B信息产品组/':
        case '赶集直招业务部/赶集产品部/产品运营部/':
        case '赶集直招业务部/赶集产品部/客户产品部/':
        case '人力资源事业群/平台支撑线/平台策略部/':
        case '人力资源事业群/平台支撑线/企服产品及运营部/':
        case '用户成功部/极速产品部/B端产品组/':
        case '营销策略部/客服策略部/数据分析组/':
        case '商业运营部/政策发展组/会员策略组/':
        case '营销线/营销策略部/大服务策略部/':
            return '产品运营';

        case '风险防控部/运营部/分析部/':
        case '风险防控部/运营部/数据部/':
        case '风险策略及运营部/用户服务部/call in部/':
        case '风险策略及运营部/用户服务部/应急处理部/':
        case '风险策略及运营部/风险防控部/防控部/':
        case '平台支撑线/风险策略及运营部/风控策略部/':
            return '风控团队';

        default:
            return '其他';
    }

}




/**
 * 数据的国际化格式化方式，三个数据用逗号分隔 如123456转换成123,456
 */
function internationalNumberFormatter(value) {
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function toPencent(num, row, index) {
    if (num == null) {
        return "-";
    }
    return (parseFloat(num) * 100).toFixed(2) + '%'
}

function changeTwoDecimalFx(x) {
    var fx = parseFloat(x);
    if (isNaN(fx)) {
        alert('function:changeTwoDecimal->parameter error');
        return false;
    }
    fx = Math.round(fx * 100) / 100;
    var s_x = fx.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

function internationalNumberShortFormatterStorage(value) {
    if (value == null || typeof value == 'undefined') {
        return "";
    }
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    var times = 0;
    while (rgx.test(x1)) {
        times++;
        //		x1 = x1.replace(rgx, '$1');
        x1 = changeTwoDecimalFx(x1 / 1024);
    }

    switch (times) {
        case 0:
            return internationalNumberFormatter(value) + "B";
        case 1:
            return x1 + "KB";
        case 2:
            return x1 + "MB";
        case 3:
            return x1 + "GB";
        case 4:
            return x1 + "TB";
        case 5:
            return x1 + "PB";
        case 6:
            return x1 + "EB";
        case 7:
            return x1 + "ZB";
        case 8:
            return x1 + "YB";
        default:
            return internationalNumberFormatter(value);
    }
}
